package com.ncp.model.biz.product;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.ncp.model.dao.DbConnection;
import com.ncp.model.entity.product.ProductBasicBean;
import com.ncp.model.entity.product.ProductBasicIndex;

public class ProductManager {
	private Connection ct=null;
	private PreparedStatement ps=null;
	private ResultSet rs=null;
	public boolean addProduct(String name,String introduction,String picture){
		boolean b=false;
		try {
			ct = DbConnection.getConnByDataSource();
			String sql="insert into PRODUCTBASIC values(null,'"+name+"','"+introduction+"','"+picture+"')";
			//System.out.println(sql);
			ps=ct.prepareStatement(sql);
			ps.executeUpdate();
			b=true;
		} catch (Exception e) {
		
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return b;
	}
	public ArrayList getAllProduct(){
		//System.out.println("kkk");
		ArrayList al=new ArrayList();
		try {
			ct = DbConnection.getConnByDataSource();
			String sql="select ProductID,ProductName,ProductDescription,ProductImage  from PRODUCTBASIC";
			ps=ct.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				ProductBasicIndex pb=new ProductBasicIndex();
				pb.setProductName(rs.getString(2));
				pb.setProductID(rs.getInt(1));
				pb.setProductDescription (rs.getString(3));
				//setPdate(rs.getTimestamp("pdate"));
				pb.setProductImage(rs.getString(4));
				al.add(pb);
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return al;
	}
	public ProductBasicIndex getProduct(String id){
		ProductBasicIndex pb=new ProductBasicIndex();
		try {
			ct = DbConnection.getConnByDataSource();
			String sql="select ProductID,ProductName,ProductDescription,ProductImage from PRODUCTBASIC where id="+id;
			ps=ct.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				pb.setProductImage(rs.getString(4));
				pb.setProductID(rs.getInt(1));
				pb.setProductName(rs.getString(2));
				pb.setProductDescription(rs.getString(3));
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return pb;
	}
	public void closeResource(){
		try {
			if(rs!=null){
				rs.close();
				rs=null;
			}
			if(ps!=null){
				ps.close();
				ps=null;
			}
			if(ct!=null){
				ct.close();
				ct=null;
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}
}
